#!/bin/sh

macaddr_add() {
	local mac=$1
	local val=$2
	local oui=${mac%:*:*:*}
	local nic=${mac#*:*:*:}

	nic=$(printf "%06x" $((0x${nic//:/} + $val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
	echo $oui:$nic
}

set_wired_lan()
{
	wan_mode=`cfg -g TZ_WIRED_WAN_MODE --section system`
	if [ "${wan_mode}" = "" ]; then
		wan_mode="0"
	fi

	apn1_action=`cfg -g TZ_APN1_ACTION --section system`
	apn2_action=`cfg -g TZ_APN2_ACTION --section system`

	apn_num=

	if [ "${apn1_action}" = "3" ]; then
		apn_num=1
	else if [ "${apn2_action}" = "3" ]; then
		apn_num=2
	else
		apn_num=0
	fi
	fi

	if [ "${apn_num}" = "0" ]; then
		if [ "${wan_mode}" = "1" ]; then
			cpeconfer network_set_wired_lan_mode lan-lan-lan-wan 0
		else
			cpeconfer network_set_wired_lan_mode lan-lan-lan-lan 0
		fi
	else
		if [ "${wan_mode}" = "1" ]; then
			cpeconfer network_set_wired_lan_mode lan-lan-iptv-wan ${apn_num}
		else
			cpeconfer network_set_wired_lan_mode lan-lan-iptv-lan ${apn_num}
		fi
	fi


}


FORM_CONFIG_STATION=/tmp/form_config/
DHCP_CONFIG_LOAD_FILE=${FORM_CONFIG_STATION}/dhcp
FACTORY_CONFIG_FILE=/etc/tozed_param

#export param
mkdir -p ${FORM_CONFIG_STATION}
cat ${FACTORY_CONFIG_FILE} | grep TZDHCP_ > ${DHCP_CONFIG_LOAD_FILE}
chmod 777 ${DHCP_CONFIG_LOAD_FILE}
. ${DHCP_CONFIG_LOAD_FILE}



DHCP_MAIN_IP=
DHCP_MAIN_MASK=
DHCP_MAIN_MAC=`eth_mac r lan`

DHCP_VICE_1_IP=
DHCP_VICE_1_MASK=
DHCP_VICE_1_MAC=$(macaddr_add "$DHCP_MAIN_MAC" 2)

DHCP_VICE_2_IP=
DHCP_VICE_2_MASK=
DHCP_VICE_2_MAC=$(macaddr_add "$DHCP_MAIN_MAC" 3)

WIRED_WAN_MAC=`eth_mac r wan`

if [ "${TZDHCP_MAIN_IP}" = "" ]; then
		DHCP_MAIN_IP=192.168.2.1
else
		DHCP_MAIN_IP=${TZDHCP_MAIN_IP}
fi

if [ "${TZDHCP_MAIN_NETMASK}" = "" ]; then
		DHCP_MAIN_MASK=255.255.255.0
else
		DHCP_MAIN_MASK=${TZDHCP_MAIN_NETMASK}
fi

if [ "${TZDHCP_VICE_1_IP}" = "" ]; then
		DHCP_VICE_1_IP=192.168.3.1
else
		DHCP_VICE_1_IP=${TZDHCP_VICE_1_IP}
fi

if [ "${TZDHCP_VICE_1_NETMASK}" = "" ]; then
		DHCP_VICE_1_MASK=255.255.255.0
else
		DHCP_VICE_1_MASK=${TZDHCP_VICE_1_NETMASK}
fi


if [ "${TZDHCP_VICE_2_IP}" = "" ]; then
		DHCP_VICE_2_IP=192.168.4.1
else
		DHCP_VICE_2_IP=${TZDHCP_VICE_2_IP}
fi

if [ "${TZDHCP_VICE_2_NETMASK}" = "" ]; then
		DHCP_VICE_2_MASK=255.255.255.0
else
		DHCP_VICE_2_MASK=${TZDHCP_VICE_2_NETMASK}
fi

cat > /etc/config/network <<EOF

config interface 'loopback'
	option ifname 'lo'
	option proto 'static'
	option ipaddr '127.0.0.1'
	option netmask '255.0.0.0'

#config globals 'globals'
#	option ula_prefix 'fd41:5d83:aaf2::/48'

config interface 'lan'
	option ifname 'eth0.1'
	option force_link '1'
	option macaddr '${DHCP_MAIN_MAC}'
	option type 'bridge'
	option proto 'static'
	option ipaddr '${DHCP_MAIN_IP}'
	option netmask '${DHCP_MAIN_MASK}'
	option ip6assign '60'

config interface 'wan'
	option ifname 'eth0.2'
	option force_link '1'
	option macaddr '${WIRED_WAN_MAC}'
	option proto 'dhcp'
	option route_lan_section 'lan'
	option land_wan '1'

config interface 'wan6'
	option ifname 'eth0.2'
    option proto 'dhcpv6'
    option route_lan_section 'lan'
    option land_wan '1'

config interface 'lan1'
	option ifname 'eth0.3'
	option force_link '1'
	option macaddr '${DHCP_VICE_1_MAC}'
	option type 'bridge'
	option proto 'static'
	option ipaddr '${DHCP_VICE_1_IP}'
	option netmask '${DHCP_VICE_1_MASK}'
	option ip6assign '60'

config interface 'lan2'
	option ifname 'eth0.4'
	option force_link '1'
	option macaddr '${DHCP_VICE_2_MAC}'
	option type 'bridge'
	option proto 'static'
	option ipaddr '${DHCP_VICE_2_IP}'
	option netmask '${DHCP_VICE_2_MASK}'
	option ip6assign '60'

config interface '4g'
    option ifname 'bmwan0'
    option force_link '1'
    option proto 'dhcp'
    option route_lan_section 'lan'
    option primary_apn '1'

config interface '4gv6'
    option ifname 'bmwan0'
    option proto 'dhcpv6'
    option primary_apn '1'
    option route_lan_section 'lan'

config interface '4g1'
    option ifname 'bmwan1'
    option force_link '1'
    option proto 'dhcp'
    option route_lan_section 'lan1'

config interface '4g1v6'
    option ifname 'bmwan1'
    option proto 'dhcpv6'
    option route_lan_section 'lan1'

config interface '4g2'
    option ifname 'bmwan2'
    option force_link '1'
    option proto 'dhcp'
    option route_lan_section 'lan2'

config interface '4g2v6'
    option ifname 'bmwan2'
    option proto 'dhcpv6'
    option route_lan_section 'lan2'

config switch
	option name 'switch0'
	option reset '1'
	option enable_vlan '1'

config switch_vlan
	option device 'switch0'
	option vlan '1'
	option ports '0 1 2 3 4 6t'

config switch_vlan
	option device 'switch0'
	option vlan '2'
	option ports '6t'

config switch_vlan
	option device 'switch0'
	option vlan '3'
	option ports '6t'

config switch_vlan
	option device 'switch0'
	option vlan '4'
	option ports '6t'

EOF

set_wired_lan